$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'car/carbaseselled/list',
        datatype: "json",
        colModel: [
            // { label: '图片', name: 'pic', index: 'pic', width: 80 },
            { label: '车辆名称', name: 'searchName', index: 'search_name', width: 80 },
            { label: '年款', name: 'carYear', index: 'car_year', width: 80 },
            { label: '排量', name: 'carDisplacement', index: 'car_displacement', width: 80 },
            { label: '上牌时间', name: 'licenseTime', index: 'license_time', width: 80 ,formatter:"date",formatoptions: {srcformat:'Y-m-d H:i:s',newformat:'Y-m-d'}},
            { label: '行驶里程', name: 'driveMileage', index: 'drive_mileage', width: 80 },
            { label: '变速箱', name: 'transmission', index: 'transmission', width: 80 },
            { label: '颜色', name: 'colourZh', index: 'colour', width: 80 },
            { label: '排放标准', name: 'emissionStandardZh', index: 'emission_standard', width: 80 },
            { label: '车型', name: 'modelZh', index: 'model', width: 80 },
            { label: '倒手次数', name: 'sellNum', index: 'sell_num', width: 80 }
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		carBase: {}
		,pfbzList:[]
		,cxList:[]
		,carColorList:[]
		,shopList:[]
		,carFriendShopList:[]
        ,lastMoney:0
	},
    watch:{
        carBase:{//深度监听，可监听到对象、数组的变化
            handler(val, oldVal){
                vm.carBase = val;
                vm.lastMoney =vm.carBase.sellPrice - vm.carBase.buyPrice - vm.carBase.otherPrice ;vm.carBase.sellPrice
                if (checkNotNull(vm.carBase.carShopMoneyEntities)){
                    for (let i = 0; i <vm.carBase.carShopMoneyEntities.length ; i++) {
                        let entity = vm.carBase.carShopMoneyEntities[i];
                        vm.lastMoney = vm.lastMoney - entity.getMoney;
                    }
                }
            },
            deep:true //true 深度监听
        }
    }
	,methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.carBase = {
                friendShopId:[]
            };

            vm.getDictForCarPfbz(1);

            $('#datetimepicker1').datetimepicker({
                format: 'YYYY-MM-DD',
                locale: moment.locale('zh-cn')
            });
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            vm.getDictForCarPfbz(0);
            setTimeout(vm.getInfo(id),100)
		},
		saveOrUpdate: function (event) {
		    $('#btnSaveOrUpdate').button('loading').delay(1000).queue(function() {
                var url = vm.carBase.id == null ? "car/carbase/save" : "car/carbase/update";
                $.ajax({
                    type: "POST",
                    url: baseURL + url,
                    contentType: "application/json",
                    data: JSON.stringify(vm.carBase),
                    success: function(r){
                        if(r.code === 0){
                             layer.msg("操作成功", {icon: 1});
                             vm.reload();
                             $('#btnSaveOrUpdate').button('reset');
                             $('#btnSaveOrUpdate').dequeue();
                        }else{
                            layer.alert(r.msg);
                            $('#btnSaveOrUpdate').button('reset');
                            $('#btnSaveOrUpdate').dequeue();
                        }
                    }
                });
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			var lock = false;
            layer.confirm('确定要删除选中的记录？', {
                btn: ['确定','取消'] //按钮
            }, function(){
               if(!lock) {
                    lock = true;
		            $.ajax({
                        type: "POST",
                        url: baseURL + "car/carbase/delete",
                        contentType: "application/json",
                        data: JSON.stringify(ids),
                        success: function(r){
                            if(r.code == 0){
                                layer.msg("操作成功", {icon: 1});
                                $("#jqGrid").trigger("reloadGrid");
                            }else{
                                layer.alert(r.msg);
                            }
                        }
				    });
			    }
             }, function(){
             });
		}
        ,editSellPrice:function () {
            var id = getSelectedRow();
            if(id == null){
                return ;
            }

            $.get(baseURL + "car/carbase/info/"+id, function(r){
                vm.carBase = r.carBase;

                layer.open({
                    type: 1,
                    offset: '50px',
                    skin: 'layui-layer-molv',
                    title: "编辑价格相关",
                    area: ['600px', '450px'],
                    shade: 0,
                    shadeClose: false,
                    content: jQuery("#sellPriceDiv"),
                    btn: ['确定', '取消'],
                    yes:function(index){
                        alert(vm.lastMoney)
                    },
                    btn1: function (index) {
                        layer.close(index);
                    }
                });

            });



        }
		,getInfo: function(id){
			$.get(baseURL + "car/carbase/info/"+id, function(r){
                vm.carBase = r.carBase;
                if (checkIsNull(vm.carBase.friendShopId)){
                    vm.carBase.friendShopId = [];
                }
                console.log(vm.carBase)
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		}
        ,getDictForCarPfbz:function (isSave) {
            vm.carBase.sellNum = 0;//倒手次数
            vm.carBase.colour = 0;//倒手次数
            vm.carBase.transmission = "手动";//倒手次数
            $.get(baseURL + "sys/dict/listAll?type=pfbz", function(r){
                vm.pfbzList = r.list;
                if (isSave == 1){
                    vm.carBase.emissionStandard = 8;
                }
            });
            $.get(baseURL + "sys/dict/listAll?type=cx", function(r){
                vm.cxList = r.list;
                if (isSave == 1) {
                    vm.carBase.model = 10;
                }
            });
            $.get(baseURL + "sys/dict/listAll?type=car_color", function(r){
                vm.carColorList = r.list;
                if (isSave == 1) {
                    vm.carBase.colour = 19;
                }
            });
            $.get(baseURL + "car/carfriendshop/list", function(r){
                vm.carFriendShopList = r.list;
            });
            $.get(baseURL + "car/carshop/list", function(r){
                vm.shopList = r.page.list;
                if (checkNotNull(vm.shopList) && isSave == 1){
                    vm.carBase.shopId = vm.shopList[0].id;
                }
            });

        }
	}
});